package itsm.isperp.framework.web.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
 * 
 * 
 * @author lizx
 * @date 2015-8-31
 * @since 1.0
 * 
 */
public class CookiesUtils {

	public static final Cookie getCookie(HttpServletRequest req,
			String cookiesName) {

		Cookie[] cookies = req.getCookies();
		if (cookies != null) {
			for (Cookie co : cookies) {
				if (cookiesName.equals(co.getName())) {
					return co;
				}
			}
		}
		return null;

	}

	public static final void putCookie(HttpServletRequest request,
			HttpServletResponse response, String name, String value) {
		Cookie cc = getCookie(request, name);
		if (cc == null) {
			cc = new Cookie(name, value);
			cc.setPath("/");
		} else {
			cc.setValue(value);
		}
		response.addCookie(cc);

	}

	public static final void setCookie(HttpServletResponse response,
			String name, String value) {
		Cookie cookie = new Cookie(name, value);
		response.addCookie(cookie);

	}
}
